package com.world.cat.controller.back;

import com.world.cat.model.back.Article;
import com.world.cat.model.sys.User;
import com.world.cat.service.back.ArticleService;
import com.world.cat.service.generate.FieldService;
import com.world.cat.service.sys.AppService;
import com.world.common.generate.auto.QMapping;
import com.world.common.pojo.Result;
import com.world.common.pojo.WorldPage;
import com.world.common.util.NumberUtil;
import com.world.common.util.SessionUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/back/article", name = "文章管理")
public class ArticleController {
    @Resource
    private ArticleService service;

    //列表页 jqxgrid组件
    @RequestMapping(value = "/home")
    public String home(Model model) {
        return "/back/article/home";
    }

    //列表页 datatable组件
    @RequestMapping(value = "/homeDatatable")
    public String homeDatatable(Model model) {
        return "/back/article/homeDatatable";
    }

    //草稿箱
    @RequestMapping(value = "/trash")
    public String homeTrash(Model model) {
        return "/back/article/trash";
    }

    //列表页数据 jqxgrid组件
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String params, HttpServletRequest request, WorldPage worldPage) throws Exception {
        worldPage.setPageIndex(NumberUtil.intValue(request.getParameter("pagenum")) + 1);
        worldPage.setPageSize(NumberUtil.intValue(request.getParameter("pagesize")));
        worldPage.setProperties("createTime");
        Page<Article> pages = service.findPage(Article.class, params, worldPage.getPageRequest());
        return pages;
    }

    //列表页数据 datatable组件
    @RequestMapping(value = "/listDataTable")
    @ResponseBody
    public Object listDataTable(String params, HttpServletRequest request) throws Exception {
        int draw = Integer.parseInt(request.getParameter("draw"));
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        WorldPage worldPage = new WorldPage();
        worldPage.setPageIndex(start + 1);
        worldPage.setPageSize(length);
        worldPage.setProperties("createTime");
        Page<Article> pages = service.findPage(Article.class, params, worldPage.getPageRequest());

        Map<String, Object> map = new HashMap<>();
        map.put("draw", draw);
        map.put("recordsTotal", pages.getTotalElements());
        map.put("recordsFiltered", pages.getTotalElements());
        map.put("data", pages.getContent());
        return map;
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(String params) throws Exception {
        Result result = service.save(Article.class, params);
        return result;
    }

    @RequestMapping(value = "/del", name = "彻底删除")
    @ResponseBody
    public Object del(String[] ids) {
        service.del(ids, false);
        return new Result(true);
    }

    @RequestMapping(value = "/delSoft", name = "软删除")
    @ResponseBody
    public Object delSoft(String[] ids) {
        service.del(ids, true);
        return new Result(true);
    }

    @RequestMapping(value = "/edit", name = "编辑")
    public String edit(Model model, String id) throws Exception {
        model.addAttribute("fieldMap", FieldService.getFieldMap(Article.class));
        Article object = service.get(id);
        model.addAttribute("obj", object);
        return "/back/article/edit";
    }

    @RequestMapping(value = "/add", name = "-发布")
    public String add(Model model) throws Exception {
        model.addAttribute("fieldMap", FieldService.getFieldMap(Article.class));
        return "/back/article/add";
    }

    @RequestMapping(value = "/view", name = "查看")
    public String view(Model model, String id) throws Exception {
        model.addAttribute("fieldMap", FieldService.getFieldMap(Article.class));
        Article object = service.get(id);
        model.addAttribute("obj", object);
        return "/back/article/view";
    }

    @QMapping(relatedUrl = "/back/article/trash", name = "草稿箱查看")
    @RequestMapping(value = "/trashView")
    public String trashView(Model model, String id) throws Exception {
        return view(model, id);
    }

    @QMapping(relatedUrl = "/back/article/trash")
    @RequestMapping(value = "/trashEdit", name = "草稿箱编辑")
    public String trashEdit(Model model, String id) throws Exception {
        return edit(model, id);
    }

    @RequestMapping(value = "/viewAll", name = "查看所有")
    public void viewAll() {
    }
}
